package com.kehutong.basic.annex.service;

import com.kehutong.basic.annex.entity.AttributeRecord;
import com.kehutong.basic.annex.enums.BusinessType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.util.Enums;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;

@WebService("/basic/attribute/record/service")
public class AttributeRecordService {

    @Inject
    private JdbcSession jdbcSession;

    public Object page(JSONObject jsonObject) {
        String businessType = jsonObject.getString("businessType");
        String businessId = jsonObject.getString("businessId");
        if (Objects.isEmpty(businessType) && Objects.isEmpty(businessId)) {
            return Result.error(0, "请输入必填参数");
        }

        return Result.success(jdbcSession.findPage(AttributeRecord.class)
                .eq(jsonObject, "businessType", "businessId")
                .order("createTime", "desc")
                .page(jsonObject)
                .exe());
    }

    @ReqMapping("/get")
    public Object get(JSONObject jsonObject) {
        List<AttributeRecord> residentHouses =  jdbcSession.findArray(AttributeRecord.class)
                .eq(jsonObject,"businessId", "businessType")
                .order("createTime", "desc")
                .limit(0,1)
                .exe();

        if(Objects.nonEmpty(residentHouses)){
            return Result.success(residentHouses.get(0));
        }

        return null;
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(AttributeRecord entity) {
        jdbcSession.updateById(entity);
        return Result.success(entity);
    }

    @ReqMapping("/save")
    public Object save(JSONObject jsonObject) {
        AttributeRecord entity = getAttributeRecord(jsonObject);
        jdbcSession.insert(entity);
        return Result.success();
    }

    private AttributeRecord getAttributeRecord(JSONObject jsonObject) {
        AttributeRecord entity = Pool.newInstance(AttributeRecord.class);

        String businessId = jsonObject.getString("businessId");
        entity.setBusinessId(businessId);

        BusinessType businessType = Enums.getEnum(BusinessType.class, jsonObject.getString("businessType"));
        entity.setBusinessType(businessType);

        entity.setCurrentNo(jsonObject.getString("currentNo"));
        entity.setCurrentName(jsonObject.getString("currentName"));
        entity.setBeforeNo(jsonObject.getString("beforeNo"));
        entity.setBeforeName(jsonObject.getString("beforeName"));
        entity.setOperateNo(jsonObject.getString("operateNo"));
        entity.setOperateName(jsonObject.getString("operateName"));
        entity.setStartTime(jsonObject.getString("startTime"));
        entity.setEndTime(jsonObject.getString("endTime"));

        JSONArray beforeNoArray = jsonObject.getJSONArray("beforeNos");
        if(Objects.nonEmpty(beforeNoArray)){
            List<String> beforeNos = toListStr(beforeNoArray);
            entity.setBeforeNos(beforeNos);
        }

        JSONArray currentNoArray = jsonObject.getJSONArray("currentNos");
        if(Objects.nonEmpty(currentNoArray)){
            List<String> currentNos = toListStr(currentNoArray);
            entity.setCurrentNos(currentNos);
        }

        return entity;
    }

    private List<String> toListStr(JSONArray array){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < array.size(); i ++) {
            String currentNo = array.getString(i);
            list.add(currentNo);
        }
        return list;
    }
}
